/*---------------------------------------------------------------------*/
/* --- STC MCU Limited ------------------------------------------------*/
/* --- STC 1T Series MCU Demo Programme -------------------------------*/
/* --- Mobile: (86)13922805190 ----------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ------------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966 ------------------------*/
/* --- Web: www.STCMCU.com --------------------------------------------*/
/* --- Web: www.STCMCUDATA.com  ---------------------------------------*/
/* --- QQ:  800003751 -------------------------------------------------*/
/* ����?�ڳ�����ʹ�ô˴���,���ڳ�����ע��ʹ����STC�����ϼ�����            */
/*---------------------------------------------------------------------*/
#include "stdio.h"
#include "config.h"
#include "adc.h"
#include "gpio.h"
#include "timer.h"
#include "I2C.h"
#include "delay.h"
#include "uart.h"

#include "rs485.h"
#include "MainTask.h"

/*************	����˵��	**************

�����̻���STC8H8K64UΪ����оƬ��ʵ����8���б�д���ԣ�STC8G��STC8Hϵ��оƬ��ͨ�òο�.

��������ʾ��·ADC��ѯ������ͨ������2���͸���λ����������115200,N,8,1��

����ʱ, ѡ��ʱ�� 22.1184MHz (�����������ļ�"config.h"���޸�).

******************************************/

/*************	���س�������	**************/

/*************	���ر�������	**************/

/*************	���غ�������	**************/

/*************  �ⲿ�����ͱ������� *****************/

/******************* IO���ú��� *******************/
void GPIO_config(void)
{
	GPIO_InitTypeDef xdata GPIO_InitStructure; //�ṹ����

	//uart 1
	GPIO_InitStructure.Pin = GPIO_Pin_0 | GPIO_Pin_1; //ָ��Ҫ��ʼ����IO, GPIO_Pin_0 ~ GPIO_Pin_7
	GPIO_InitStructure.Mode = GPIO_PullUp;			  //ָ��IO������������ʽ,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
	GPIO_Inilize(GPIO_P3, &GPIO_InitStructure);		  //��ʼ��

	//uart 2
	GPIO_InitStructure.Pin = GPIO_Pin_0 | GPIO_Pin_1; //ָ��Ҫ��ʼ����IO, GPIO_Pin_0 ~ GPIO_Pin_7
	GPIO_InitStructure.Mode = GPIO_PullUp;			  //ָ��IO������������ʽ,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
	GPIO_Inilize(GPIO_P1, &GPIO_InitStructure);		  //��ʼ��

	//485
	GPIO_InitStructure.Pin = GPIO_Pin_2;		//ָ��Ҫ��ʼ����IO, GPIO_Pin_0 ~ GPIO_Pin_7
	GPIO_InitStructure.Mode = GPIO_OUT_PP;		//ָ��IO������������ʽ,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
	GPIO_Inilize(GPIO_P1, &GPIO_InitStructure); //��ʼ��

	//AD������Ϊ�����?
	GPIO_InitStructure.Pin = GPIO_Pin_3 | GPIO_Pin_2;		//ָ��Ҫ��ʼ����IO, GPIO_Pin_0 ~ GPIO_Pin_7
	GPIO_InitStructure.Mode = GPIO_HighZ;		//ָ��IO������������ʽ,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
	GPIO_Inilize(GPIO_P0, &GPIO_InitStructure); //��ʼ��

	//I2C
	GPIO_InitStructure.Pin = GPIO_Pin_2 | GPIO_Pin_3; //ָ��Ҫ��ʼ����IO, GPIO_Pin_0 ~ GPIO_Pin_7, �����?
	GPIO_InitStructure.Mode = GPIO_PullUp;			  //ָ��IO������������ʽ,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
	GPIO_Inilize(GPIO_P3, &GPIO_InitStructure);		  //��ʼ��

	//
	GPIO_InitStructure.Pin = GPIO_Pin_1;		//ָ��Ҫ��ʼ����IO, GPIO_Pin_0 ~ GPIO_Pin_7
	GPIO_InitStructure.Mode = GPIO_OUT_PP;		//ָ��IO������������ʽ,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
	GPIO_Inilize(GPIO_P0, &GPIO_InitStructure); //��ʼ��

	GPIO_InitStructure.Pin = GPIO_Pin_7 | GPIO_Pin_6; //ָ��Ҫ��ʼ����IO, GPIO_Pin_0 ~ GPIO_Pin_7
	GPIO_InitStructure.Mode = GPIO_HighZ;			  //ָ��IO������������ʽ,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
	GPIO_Inilize(GPIO_P2, &GPIO_InitStructure);		  //��ʼ��

	GPIO_InitStructure.Pin = GPIO_Pin_0;		//ָ��Ҫ��ʼ����IO, GPIO_Pin_0 ~ GPIO_Pin_7
	GPIO_InitStructure.Mode = GPIO_HighZ;		//ָ��IO������������ʽ,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
	GPIO_Inilize(GPIO_P0, &GPIO_InitStructure); //��ʼ��
	//�̵���
	GPIO_InitStructure.Pin = GPIO_Pin_0 | GPIO_Pin_1; //ָ��Ҫ��ʼ����IO, GPIO_Pin_0 ~ GPIO_Pin_7
	GPIO_InitStructure.Mode = GPIO_OUT_PP;			  //ָ��IO������������ʽ,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
	GPIO_Inilize(GPIO_P2, &GPIO_InitStructure);		  //��ʼ��

	GPIO_InitStructure.Pin = GPIO_Pin_6;		//ָ��Ҫ��ʼ����IO, GPIO_Pin_0 ~ GPIO_Pin_7
	GPIO_InitStructure.Mode = GPIO_PullUp;		//ָ��IO������������ʽ,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
	GPIO_Inilize(GPIO_P3, &GPIO_InitStructure); //��ʼ��

	GPIO_InitStructure.Pin = GPIO_Pin_5;		//ָ��Ҫ��ʼ����IO, GPIO_Pin_0 ~ GPIO_Pin_7
	GPIO_InitStructure.Mode = GPIO_OUT_PP;		//ָ��IO������������ʽ,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
	GPIO_Inilize(GPIO_P3, &GPIO_InitStructure); //��ʼ��

	//��Դ����CPS_IN
	GPIO_InitStructure.Pin = GPIO_Pin_7;		//ָ��Ҫ��ʼ����IO, GPIO_Pin_0 ~ GPIO_Pin_7
	GPIO_InitStructure.Mode = GPIO_OUT_PP;		//ָ��IO������������ʽ,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
	GPIO_Inilize(GPIO_P1, &GPIO_InitStructure); //��ʼ��
}

/******************** INT���� ********************/
// void Exti_config(void)
// {
// 	EXTI_InitTypeDef xdata Exti_InitStructure; //�ṹ����

// 	Exti_InitStructure.EXTI_Interrupt = ENABLE;	  //�ж�ʹ��,   ENABLE��DISABLE
// 	Exti_InitStructure.EXTI_Mode = EXT_MODE_Fall; //�ж�ģʽ,   EXT_MODE_RiseFall,EXT_MODE_Fall
// 	Exti_InitStructure.EXTI_Polity = Polity_3;	  //ָ���ж����ȼ�(�͵���) Polity_0,Polity_1,Polity_2,Polity_3
// 	Ext_Inilize(EXT_INT0, &Exti_InitStructure);	  //��ʼ��

// Exti_InitStructure.EXTI_Interrupt = ENABLE;				//�ж�ʹ��,   ENABLE��DISABLE
// Exti_InitStructure.EXTI_Mode      = EXT_MODE_Fall;		//�ж�ģʽ,   EXT_MODE_RiseFall,EXT_MODE_Fall
// Exti_InitStructure.EXTI_Polity    = Polity_0;			//ָ���ж����ȼ�(�͵���) Polity_0,Polity_1,Polity_2,Polity_3
// Ext_Inilize(EXT_INT1,&Exti_InitStructure);				//��ʼ��

// Exti_InitStructure.EXTI_Interrupt = ENABLE;				//�ж�ʹ��,   ENABLE��DISABLE
// Ext_Inilize(EXT_INT2,&Exti_InitStructure);				//��ʼ��

// Exti_InitStructure.EXTI_Interrupt = ENABLE;				//�ж�ʹ��,   ENABLE��DISABLE
// Ext_Inilize(EXT_INT3,&Exti_InitStructure);				//��ʼ��

// Exti_InitStructure.EXTI_Interrupt = ENABLE;				//�ж�ʹ��,   ENABLE��DISABLE
// Ext_Inilize(EXT_INT4,&Exti_InitStructure);				//��ʼ��
// }

/******************* AD���ú��� *******************/
void ADC_config(void)
{
	ADC_InitTypeDef xdata ADC_InitStructure;			   //�ṹ����
	ADC_InitStructure.ADC_SMPduty = 31;					   //ADC ģ���źŲ���ʱ�����?, 0~31��ע�⣺ SMPDUTY һ����������С�� 10��
	ADC_InitStructure.ADC_CsSetup = 0;					   //ADC ͨ��ѡ��ʱ�����? 0(Ĭ��),1
	ADC_InitStructure.ADC_CsHold = 1;					   //ADC ͨ��ѡ�񱣳�ʱ�����? 0,1(Ĭ��),2,3
	ADC_InitStructure.ADC_Speed = ADC_SPEED_2X1T;		   //���� ADC ����ʱ��Ƶ��	ADC_SPEED_2X1T~ADC_SPEED_2X16T
	ADC_InitStructure.ADC_Power = ENABLE;				   //ADC��������/�ر�	ENABLE,DISABLE
	ADC_InitStructure.ADC_AdjResult = ADC_RIGHT_JUSTIFIED; //ADC�������?,	ADC_LEFT_JUSTIFIED,ADC_RIGHT_JUSTIFIED
	ADC_InitStructure.ADC_Polity = Polity_3;			   //ָ���ж����ȼ�(�͵���) Polity_0,Polity_1,Polity_2,Polity_3
	ADC_InitStructure.ADC_Interrupt = DISABLE;			   //�ж�����	ENABLE,DISABLE
	ADC_Inilize(&ADC_InitStructure);					   //��ʼ��
	ADC_PowerControl(ENABLE);							   //������ADC��Դ��������, ENABLE��DISABLE
}

/***************  ���ڳ�ʼ������ *****************/
void UART_config(void)
{
	COMx_InitDefine xdata COMx_InitStructure;		 //�ṹ����
	//uart1
	COMx_InitStructure.UART_Mode = UART_8bit_BRTx;	 //ģʽ, UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
	COMx_InitStructure.UART_BRT_Use = BRT_Timer1;	 //ʹ�ò�����, BRT_Timer1, BRT_Timer2 (ע��: ����2�̶�ʹ��BRT_Timer2)
	COMx_InitStructure.UART_BaudRate = 115200ul;	 //������, һ�� 110 ~ 115200
	COMx_InitStructure.UART_RxEnable = ENABLE;		 //��������,   ENABLE��DISABLE
	COMx_InitStructure.BaudRateDouble = DISABLE;	 //�����ʼӱ�, ENABLE��DISABLE
	COMx_InitStructure.UART_Interrupt = ENABLE;		 //�ж�����,   ENABLE��DISABLE
	COMx_InitStructure.UART_Polity = Polity_1;		 //ָ���ж����ȼ�(�͵���) Polity_0,Polity_1,Polity_2,Polity_3
	COMx_InitStructure.UART_P_SW = UART1_SW_P30_P31; //�л��˿�,   UART1_SW_P30_P31,UART1_SW_P36_P37,UART1_SW_P16_P17,UART1_SW_P43_P44
	UART_Configuration(UART1, &COMx_InitStructure);	 //��ʼ������1 UART1,UART2,UART3,UART4

	//uart2
	COMx_InitStructure.UART_Mode = UART_8bit_BRTx;	 //ģʽ,   UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
	COMx_InitStructure.UART_BRT_Use = BRT_Timer2;	 //ʹ�ò�����, BRT_Timer1, BRT_Timer2 (ע��: ����2�̶�ʹ��BRT_Timer2)
	COMx_InitStructure.UART_BaudRate = 115200ul;	 //������,     110 ~ 115200
	COMx_InitStructure.UART_RxEnable = ENABLE;		 //��������,   ENABLE��DISABLE
	COMx_InitStructure.UART_Interrupt = ENABLE;		 //�ж�����,   ENABLE��DISABLE
	COMx_InitStructure.UART_Polity = Polity_2;		 //ָ���ж����ȼ�(�͵���) Polity_0,Polity_1,Polity_2,Polity_3
	COMx_InitStructure.UART_P_SW = UART2_SW_P10_P11; //�л��˿�,   UART2_SW_P10_P11,UART2_SW_P46_P47
	UART_Configuration(UART2, &COMx_InitStructure);	 //��ʼ������2 USART1,USART2,USART3,USART4
}

/************************ ��ʱ������ ****************************/
void Timer_config(void)
{
	//timer0
	TIM_InitTypeDef xdata TIM_InitStructure;					 //�ṹ����
	TIM_InitStructure.TIM_Mode = TIM_16BitAutoReload;			 //ָ������ģʽ,   TIM_16BitAutoReload,TIM_16Bit,TIM_8BitAutoReload,TIM_16BitAutoReloadNoMask
	TIM_InitStructure.TIM_Polity = Polity_3;					 //ָ���ж����ȼ�(�͵���) Polity_0,Polity_1,Polity_2,Polity_3
	TIM_InitStructure.TIM_Interrupt = ENABLE;					 //�ж��Ƿ�����,   ENABLE��DISABLE
	TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_1T;				 //ָ��ʱ��Դ,     TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_Ext
	TIM_InitStructure.TIM_ClkOut = DISABLE;						 //�Ƿ������������?, ENABLE��DISABLE
	TIM_InitStructure.TIM_Value = 65536UL - (MAIN_Fosc / 1000);  //�ж�Ƶ��, 1000��/��
	TIM_InitStructure.TIM_Run = ENABLE;							 //�Ƿ��ʼ����������ʱ��?, ENABLE��DISABLE
	Timer_Inilize(Timer0, &TIM_InitStructure);					 //��ʼ��Timer0	  Timer0,Timer1,Timer2,Timer3,Timer4
	//timer4
	// TIM_InitStructure.TIM_Mode = TIM_16BitAutoReload;			//ָ������ģʽ,   TIM_16BitAutoReload,TIM_16Bit,TIM_8BitAutoReload,TIM_16BitAutoReloadNoMask
	// TIM_InitStructure.TIM_Polity = Polity_3;						//ָ���ж����ȼ�(�͵���) Polity_0,Polity_1,Polity_2,Polity_3
	// TIM_InitStructure.TIM_Interrupt = ENABLE;					//�ж��Ƿ�����,   ENABLE��DISABLE
	// TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_1T;				//ָ��ʱ��Դ,     TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_Ext
	// TIM_InitStructure.TIM_ClkOut = DISABLE;						//�Ƿ������������?, ENABLE��DISABLE
	// TIM_InitStructure.TIM_Value = 65536UL - (MAIN_Fosc / 1000);	//�ж�Ƶ��, 1000��/��
	// TIM_InitStructure.TIM_Run = ENABLE;							//�Ƿ��ʼ����������ʱ��?, ENABLE��DISABLE
	// Timer_Inilize(Timer4, &TIM_InitStructure);					//��ʼ��Timer0	  Timer0,Timer1,Timer2,Timer3,Timer4
}

/****************  I2C��ʼ������ *****************/
void I2C_config(void)
{
	I2C_InitTypeDef xdata I2C_InitStructure;
	I2C_InitStructure.I2C_Mode = I2C_Mode_Master; //����ѡ��   I2C_Mode_Master, I2C_Mode_Slave
	I2C_InitStructure.I2C_Enable = ENABLE;		  //I2C����ʹ��,   ENABLE, DISABLE
	I2C_InitStructure.I2C_MS_WDTA = DISABLE;	  //����ʹ���Զ�����,  ENABLE, DISABLE
	I2C_InitStructure.I2C_MS_Interrupt = DISABLE; //ʹ������ģʽ�ж�,  ENABLE, DISABLE
	I2C_InitStructure.I2C_Speed = 16;			  //�����ٶ�=Fosc/2/(Speed*2+4),      0~63
	I2C_InitStructure.I2C_IoUse = I2C_P33_P32;	  //IO���л�   I2C_P14_P15, I2C_P24_P25, I2C_P33_P32

	I2C_Init(&I2C_InitStructure);
}

/**********************************************/

void main(void)
{
	P17 = 0;
	P20 = 0;
	P21 = 0;
	P35 = 0;
	RS485EN = 0;
	GPIO_config();
	RS485EN = 0;
	P17 = 1;
	P20 = 0;
	P35 = 0;
	P21 = 0;

	ADC_config();
	UART_config();
	I2C_config();
	Timer_config();
	AppInit();
	EA = 1;
	
	delay_ms(10);
	// printf("Start\r\n");
	while (1)
	{
		MainTask();
	}
}
